package 
{
	import buttons.*;
	
	import flash.display.Loader;
	import flash.display.Sprite;
	import flash.events.*;
	import flash.net.*;

	public class PhotoWalletView extends Sprite
	{
		
		// Events dispatched to the controller, which then initiates model functionality
		public static const DISPLAY_BACKGROUND:uint = 0xcccccc;
		public static const DISPLAY_WIDTH:Number = 340;; 
		public static const DISPLAY_HEIGHT:Number = 340;
		public static const DISPLAY_PADDING:Number = 10;
		
		public static const PHOTO_CONTAINER_BACKGROUND:uint = 0xeeeeee;
		public static const PHOTO_CONTAINER_WIDTH:Number = 320;
		public static const PHOTO_CONTAINER_HEIGHT:Number = 280;
		
		public static const BUTTON_WIDTH:Number = 100;
		public static const BUTTON_HEIGHT:Number = 30;
		
		public static const NEXT_CLICKED:String = 'NEXT_CLICKED';
		public static const PREVIOUS_CLICKED:String = 'PREVIOUS_CLICKED';
		public static const PLAY_CLICKED:String = 'PLAY_CLICKED';
		
		private var _photoModel:PhotoWalletModel;
		private var _displayContainer:Sprite;
		private var _photoContainer:Sprite;
		
		private var _photos:Array;
		private var _currentPhoto:Loader;
		private var _photoMaxWidth:Number;
		private var _photoMaxHeight:Number;		
		
		private var _next:ForwardButton;
		private var _previous:BackButton;
		private var _play:TextButton;
		
		private var _firstLoaded:Boolean = false; 
				
		public function PhotoWalletView( photoModel:PhotoWalletModel ) {
			
			_photoMaxWidth = PhotoWalletView.PHOTO_CONTAINER_WIDTH - 
										( PhotoWalletView.DISPLAY_PADDING * 2 );
			_photoMaxHeight = PhotoWalletView.PHOTO_CONTAINER_HEIGHT - 
										( PhotoWalletView.DISPLAY_PADDING * 2 );
			createContainers();
			createButtons();
			_photoModel = photoModel;
			loadPhotos();
			_photoModel.addEventListener( PhotoWalletModel.PHOTO_CHANGE, addPhotoToDisplay );
			
		}
		
		private function createContainers():void {
			
			_displayContainer = new Sprite();
			_displayContainer.graphics.beginFill( PhotoWalletView.DISPLAY_BACKGROUND );
			_displayContainer.graphics.drawRect( 0, 0, PhotoWalletView.DISPLAY_WIDTH, 
										PhotoWalletView.DISPLAY_HEIGHT );
			_displayContainer.graphics.endFill();
			
			addChild( _displayContainer );
			
			_photoContainer = new Sprite;
			_photoContainer.graphics.beginFill( PhotoWalletView.PHOTO_CONTAINER_BACKGROUND );
			_photoContainer.graphics.drawRoundRect( 0, 0, PhotoWalletView.PHOTO_CONTAINER_WIDTH,
										PhotoWalletView.PHOTO_CONTAINER_HEIGHT, 2, 2 );
			_photoContainer.graphics.endFill();
			_photoContainer.x = ( PhotoWalletView.DISPLAY_WIDTH - PhotoWalletView.PHOTO_CONTAINER_WIDTH )
										/ 2;
			_photoContainer.y = ( PhotoWalletView.DISPLAY_PADDING * 2 )
										+ PhotoWalletView.BUTTON_HEIGHT;
			
			_displayContainer.addChild( _photoContainer );
			
		}
		
		private function createButtons():void {
			
			_previous = new BackButton( PhotoWalletView.BUTTON_WIDTH, 
											PhotoWalletView.BUTTON_HEIGHT );
			_previous.x = PhotoWalletView.DISPLAY_PADDING;
			_previous.y = PhotoWalletView.DISPLAY_PADDING;
			_previous.addEventListener( MouseEvent.CLICK, dispatchPrevious );
			_displayContainer.addChild( _previous );
			
			_next = new ForwardButton( PhotoWalletView.BUTTON_WIDTH, 
											PhotoWalletView.BUTTON_HEIGHT );
			_next.x = ( PhotoWalletView.DISPLAY_PADDING * 2 ) + PhotoWalletView.BUTTON_WIDTH;
			_next.y = PhotoWalletView.DISPLAY_PADDING;
			_next.addEventListener( MouseEvent.CLICK, dispatchNext );
			_displayContainer.addChild( _next );
			
			_play = new TextButton( 'play', PhotoWalletView.BUTTON_WIDTH, 
											PhotoWalletView.BUTTON_HEIGHT );
			_play.x = ( PhotoWalletView.DISPLAY_PADDING * 3 ) + ( PhotoWalletView.BUTTON_WIDTH * 2 );
			_play.y = PhotoWalletView.DISPLAY_PADDING;
			_play.addEventListener( MouseEvent.CLICK, dispatchPlay );
			_displayContainer.addChild( _play );			
			
		}
		
		private function loadPhotos():void {
			
			// The idea here is to actively load all photos available
			// in the list so that there is a minimal delay between
			// photos. Memory management issues may cause me to have
			// to modify this in the future ( large lists might be
			// memory hogs ). 	
			_photos = new Array();
				
			for( var i:int=0; i<_photoModel.getPhotoListLength(); i++ ) {
				
				_photos[i] = new Loader();
				_photos[i].load( new URLRequest( _photoModel.getPhotoURL( i ) ) );
						
			}
			
			_photos[0].contentLoaderInfo.addEventListener( Event.COMPLETE, addPhotoToDisplay );
		
		
		/*	_currentPhoto = new Loader();
			_currentPhoto.x = PhotoWalletView.DISPLAY_PADDING;
			_currentPhoto.y = PhotoWalletView.DISPLAY_PADDING;
			_currentPhoto.load( new URLRequest( _photoModel.getPhotoURL( _photoModel.getCurrentPhoto() ) ));
			_currentPhoto.contentLoaderInfo.addEventListener( Event.COMPLETE, addPhotoToDisplay );
		*/	
			
		}
		
		private function addPhotoToDisplay( e:Event = null ):void {
			
			if( _photoContainer.numChildren > 0 ) {
			
				for( var i:int = _photoContainer.numChildren - 1; i >= 0; i-- ) {
				
					_photoContainer.removeChildAt( i );
				
				}
			
			}
			
			_currentPhoto = _photos[ _photoModel.getCurrentPhoto() ];
					
			if( ( _currentPhoto.width > _photoMaxWidth ) || ( _currentPhoto.height > _photoMaxHeight ) ) {
				
				var percentChange:Number;
				
				if( _currentPhoto.width > _currentPhoto.height ) {
					
					percentChange = _photoMaxWidth / _currentPhoto.width;
										
				} else {
					
					percentChange = _photoMaxHeight / _currentPhoto.height;
					
					
					
				}
				
				_currentPhoto.width = _currentPhoto.width * percentChange;
				_currentPhoto.height = _currentPhoto.height * percentChange;
				
			}
			
			_currentPhoto.x = ( ( _photoMaxWidth - _currentPhoto.width ) / 2 ) 
												+ PhotoWalletView.DISPLAY_PADDING;
			_currentPhoto.y = ( ( _photoMaxHeight - _currentPhoto.height ) / 2 )
												+ PhotoWalletView.DISPLAY_PADDING;
			
			_photoContainer.addChild( _currentPhoto );
			
		}
		
		private function dispatchNext( e:Event ):void {
		
			dispatchEvent( new Event( PhotoWalletView.NEXT_CLICKED ) );
			
		}
		
		private function dispatchPrevious( e:Event ):void {
			
			dispatchEvent( new Event (PhotoWalletView.PREVIOUS_CLICKED ) );
			
		}
		
		private function dispatchPlay( e:Event ):void {
			
			dispatchEvent( new Event( PhotoWalletView.PLAY_CLICKED ) );
			
		}
		
	}
}
